using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using BTS.DB;
using BTS.Util;

/// <summary>
/// Summary description for User
/// </summary>
/// 

namespace BTS.Entity
{

    public class Student : CommonEntity
    {

        public int _studentID;
        public string _firstname;
        public string _surname;
        public string _nickname;
        public string _school;
        public int _level;
        public string _sex;
        public DateTime _birthday;
        public string _addr;
        public string _tel;
        public string _email;
        public string _img;
        public DateTime _create_date;
        public bool _isActive;

        public Student()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public static Student CreateForm(OdbcDataReader reader)
        {
            Student student = new Student();
            Student.CreateForm(reader, student);
            return student;
        }

        
        public static bool CreateForm(OdbcDataReader reader, Student student)
        {           
            int fCount = reader.FieldCount;
            for (int i = 0; i < fCount; i++)
            {
                string name = reader.GetName(i);

                // Map to DB field. Need to change if db changed
                switch (name) {
                    case "student_id": student._studentID = reader.GetInt32(i);
                                      break;
                    case "sex": student._sex = reader.GetString(i);
                                      break;
                    case "tel": student._tel = reader.GetString(i);
                                      break;
                    case "email": student._email = reader.GetString(i);
                                      break;
                    case "addr": student._addr = reader.GetString(i);
                                      break;
                    case "birthday": student._birthday = new DateTime(reader.GetDate(i).Ticks);
                                      break;
                    case "firstname": student._firstname = reader.GetString(i);
                                      break;
                    case "surname": student._surname = reader.GetString(i);
                                      break;
                    case "nickname": student._nickname = reader.GetString(i);
                                      break;                    
                    case "school": student._school = reader.GetString(i);
                                      break;
                    case "level": student._level = reader.GetInt32(i);
                                      break;
                    case "image": student._img = reader.GetString(i);
                                      break;
                    case "create_date": student._create_date = new DateTime(reader.GetDate(i).Ticks);
                                      break;
                    case "is_active": student._isActive = reader.GetInt32(i) > 0 ? true : false;
                                      break;

                }
            }
            return reader.HasRows;

        }

        public static string GetQSearchSQL(string qsearch)
        {
            if (qsearch == null) return "";
            return String.Format(" student_id LIKE '%{0}%' or firstname LIKE '%{0}%' or surname LIKE '%{0}%' or nickname LIKE '%{0}%' or school LIKE '%{0}%' or tel LIKE '%{0}%' or email LIKE '%{0}%' or addr LIKE '%{0}%'", qsearch);
        }


        public override bool AddToDB(DBManager db)
        {
            String[] key = { "firstname", "surname", "nickname", "tel", "email", "sex", "birthday", "addr", "image", "school", "level", "create_date" };
            String[] val = { _firstname, _surname, _nickname, _tel, _email, _sex, StringUtil.ConvertYearToEng(_birthday, "yyyy/MM/dd"), _addr, _img, _school, _level.ToString(), StringUtil.ConvertYearToEng(_create_date, "yyyy/MM/dd") };
            return (db.Insert("student", key, val) > 0) ? true : false;
        }

        public override bool UpdateToDB(DBManager db)
        {
            if (_studentID <= 0) return false;
            String[] key = { "firstname", "surname", "nickname", "tel", "email", "sex", "birthday", "addr", "image", "school", "level", "create_date" };
            String[] val = { _firstname, _surname, _nickname, _tel, _email, _sex, StringUtil.ConvertYearToEng(_birthday, "yyyy/MM/dd"), _addr, _img, _school, _level.ToString(), StringUtil.ConvertYearToEng(_create_date, "yyyy/MM/dd") };
            return (db.Update("student", key, val, "student_id=" + _studentID) > 0) ? true : false;
        }

        public override bool DeleteToDB(DBManager db)
        {
            if (_studentID <= 0) return false;
            return (db.Delete("student", "student_id=" + _studentID) > 0) ? true : false;
        }

        public override bool LoadFromDB(DBManager db, string sqlCriteria)
        {            
            return LoadFromDBCustom(db, "SELECT * FROM student WHERE " + sqlCriteria);
        }

        public bool LoadFromDBCustom(DBManager db, string sqlAll)
        {
            OdbcDataReader reader = db.Query(sqlAll);
            if (!reader.Read()) return false;
            return Student.CreateForm(reader, this);
        }

        public static Student[] LoadListFromDB(DBManager db, string sqlCriteria)
        {
            return LoadListFromDB(db, "SELECT * FROM student " + sqlCriteria);
        }
        
        public static Student[] LoadListFromDBCustom(DBManager db, string sqlAll)
        {
            OdbcDataReader reader = db.Query(sqlAll);
            LinkedList<Student> list = new LinkedList<Student>();
            while (reader.Read())
            {
                list.AddLast(Student.CreateForm(reader));
            }

            Student[] entities = new Student[list.Count];
            int i = 0;
            foreach (Student s in list)
            {
                entities[i++] = s;
            }
            return entities;
        }
        
        public static string GetStudentID(int id)
        {
            return GetStudentID(id.ToString());
        }

        public static string GetStudentID(string id)
        {
            return Config.STUDENT_CODE_PREFIX + StringUtil.FillString(id, "0", Config.STUDENT_CODE_LENGTH, true);
        }
    
        public override string ToString()
        {
            return String.Format("Student [student_id={0}, firstname={1}, surname={2} ,nickname={3}, sex={4}, birthday={5}, addr={6}, tel={7}, email={8}, school={9}, level={10}]"
                                ,_studentID, _firstname, _surname, _nickname, _sex, _birthday, _addr, _tel, _email, _school, _level);
        }
    }
}
